home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0027_Seeking a TEXT line.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-11-02  |  2.6 KB  |  90 lines

  1. { GUY MCLOUGHLIN }
  2.  
  3. (* Public domain text-file "seek" line demo.            *)
  4. (* Guy McLoughlin - October 1993.                       *)
  5. program SeekLineDemo;
  6.  
  7. (* Text buffer type definition.                         *)
  8. type
  9.   TextBuffer = array[1..(16 * 1024)] of byte;
  10.  
  11.   (***** Check for IO file errors.                                    *)
  12.   (*                                                                  *)
  13. procedure CheckForErrors;
  14. var
  15.   Error : byte;
  16. begin
  17.   Error := ioresult;
  18.   if (Error <> 0) then
  19.   begin
  20.     writeln('FILE ERROR = ', Error);
  21.     halt(1);
  22.   end;
  23. end;
  24.  
  25. (***** Seek to specified line in a text file. LineCount returns the *)
  26. (*     line number that was "seeked" to.                            *)
  27. (*                                                                  *)
  28. procedure SeekLine({input } var TextFile   : text;
  29.                             var Tbuffer    : TextBuffer;
  30.                                 LineNumber : word;
  31.                    {output} var LineCount  : word);
  32. var
  33.   TempStr  : string;
  34. begin
  35.   (* Assign text buffer.                                  *)
  36.   settextbuf(TextFile, Tbuffer);
  37.  
  38.   (* Reset text file, and check for IO errors.            *)
  39.   {$I-}
  40.   reset(TextFile);
  41.   {$I+}
  42.   CheckForErrors;
  43.  
  44.   (* Read text file until just before specified line, or  *)
  45.   (* end of text file reached.                            *)
  46.   LineCount := 0;
  47.   repeat
  48.     readln(TextFile, TempStr);
  49.     inc(LineCount)
  50.   until (LineCount = pred(LineNumber)) or eof(TextFile);
  51.  
  52.   (* If end of text file not reached, add 1 to LineCount. *)
  53.   if NOT eof(TextFile) then
  54.     inc(LineCount)
  55. end;
  56.  
  57. var
  58.   LineCount,
  59.   LineNumber : word;
  60.   TempStr    : string;
  61.   TextFile   : text;
  62.   Tbuffer    : TextBuffer;
  63.  
  64. BEGIN
  65.   (* Assign text filename.                                *)
  66.   assign(TextFile, 'TEST.TXT');
  67.  
  68.   (* Obtain line numbe to display from user.              *)
  69.   write('ENTER LINE NUMBER TO DISPLAY : ');
  70.   readln(LineNumber);
  71.   writeln('SEEKING TO LINE ', LineNumber);
  72.  
  73.   (* Seek to line user wants to see.                      *)
  74.   SeekLine(TextFile, Tbuffer, LineNumber, LineCount);
  75.  
  76.   (* If seek was successful, then read and display line.  *)
  77.   if (LineCount = LineNumber) then
  78.   begin
  79.     readln(TextFile, TempStr);
  80.     writeln;
  81.     writeln('LINE ', LineNumber, ' = ', TempStr);
  82.   end
  83.   else
  84.     (* Else, display total number of lines in text file.    *)
  85.     writeln('Sorry, total lines in TEST.TXT = ', LineCount);
  86.  
  87.   (* Close the text file.                                 *)
  88.   close(TextFile);
  89. END.
  90.